package xiao_jin.fantastic_journey.core.common.item;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.predicate.entity.EntityPredicates;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
import xiao_jin.fantastic_journey.util.ModUtil;

public class LegacyButton extends Item {
    public LegacyButton(Settings settings) {
        super(settings);
    }

    @Override
    public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
        var entities = world.getEntitiesByClass(LivingEntity.class, ModUtil.getBoxByRange(player.getPos(), 5), EntityPredicates.VALID_ENTITY);
        entities.forEach(it -> {
            var maxHealFlag = world.random.nextBoolean();
            var shouldHealFlag = world.random.nextBoolean();

            var health = (maxHealFlag ? it.getMaxHealth() : it.getHealth()) * (shouldHealFlag ? 1 : -1);
            it.setHealth(it.getHealth() + health);
        });

        return TypedActionResult.pass(player.getStackInHand(hand));
    }
}

